package com.eurasia.assignment.infrastructure.entity;

import java.time.LocalDateTime;

import com.eurasia.assignment.domain.ProjectStatus;
import jakarta.persistence.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import com.eurasia.assignment.domain.Project;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 项目持久化实体
 */
@Entity
@Table(name = "projects")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ProjectEntity {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String name;
    
    @Column(nullable = false)
    private String type;

    @Column(name = "course_id", nullable = false)
    private String courseId;
    
    @Column(nullable = false)
    @Enumerated(EnumType.STRING)
    private ProjectStatus status;
    
    @Column(nullable = true)
    private Integer points;

    @Column(nullable = true)
    private String creator;

    @Column(name = "start_time", nullable = true)
    private LocalDateTime startTime;
    
    @Column(name = "finish_time", nullable = true)
    private LocalDateTime finishTime;
    
    @Column(nullable = true, length = 1000)
    private String comment;
    
    @Column(name = "file_id", nullable = true)
    private Long fileId;

    @CreationTimestamp
    @Column(name = "create_time", nullable = false, updatable = false)
    private LocalDateTime createTime;

    @UpdateTimestamp
    @Column(name = "update_time", nullable = false)
    private LocalDateTime updateTime;

    /**
     * 从领域对象创建持久化对象
     */
    public static ProjectEntity fromDomain(Project project) {
        ProjectEntity entity = new ProjectEntity();
        entity.setId(project.getId());
        entity.setName(project.getName());
        entity.setType(project.getType());
        entity.setCourseId(project.getCourse());
        entity.setStatus(project.getStatus());
        entity.setPoints(project.getPoints());
        entity.setCreator(project.getCreator());
        entity.setStartTime(project.getStartTime());
        entity.setFinishTime(project.getFinishTime());
        entity.setComment(project.getComment());
        entity.setFileId(project.getFileId());
        entity.setCreateTime(project.getCreateTime());
        entity.setUpdateTime(project.getUpdateTime());
        return entity;
    }
    
    /**
     * 转换为领域对象
     */
    public Project toDomain() {
        Project project = new Project();
        project.setId(this.id);
        project.setName(this.name);
        project.setType(this.type);
        project.setCourse(this.courseId);
        project.setStatus(this.status);
        project.setPoints(this.points);
        project.setCreator(this.creator);
        project.setStartTime(this.startTime);
        project.setFinishTime(this.finishTime);
        project.setComment(this.comment);
        project.setFileId(this.fileId);
        project.setCreateTime(this.createTime);
        project.setUpdateTime(this.updateTime);
        return project;
    }
} 